package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.login.UpdatePwdBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.PwdUtil;

/**
 * 修改密码  processor
 * @Author: zhushanshuo
 * @Date: 2020/03/10 17:30
 */
@Component("pwdUpdateProcessor")
public class PwdUpdateProcessor extends AbstractProcessor<Object, UpdatePwdBo> implements Processor<Object, UpdatePwdBo> {
	
	@Autowired
    private UserDao userDao;
    
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
	
    /**
     * 校验
     */
    @Override
    public void check(UpdatePwdBo bo) throws AabException {
    	Long userId = bo.getConcurrentUserId();
    	String oldPwd = bo.getOldPwd();
    	String newPwd = bo.getNewPwd();
    	String reNewPwd = bo.getReNewPwd();
    	if (oldPwd == null || "".equals(oldPwd.trim())){
            throw new AabException(ExceptionCode.旧密码为空);
        }
        if (newPwd == null || "".equals(newPwd.trim())){
        	throw new AabException(ExceptionCode.新密码为空);
        }
        if (reNewPwd == null || "".equals(reNewPwd.trim())){
        	throw new AabException(ExceptionCode.再次新密码为空);
        }
        if (!newPwd.equals(reNewPwd)) {
        	throw new AabException(ExceptionCode.新密码不一致);
        }
        DetailUserVo user = (DetailUserVo) userRead.detail(userId);
        String pwdDb = user.getPwd();
        oldPwd = PwdUtil.md5(oldPwd);
        newPwd = PwdUtil.md5(newPwd);
        if (!pwdDb.equals(oldPwd)) {
        	throw new AabException(ExceptionCode.旧密码错误);
        }
        bo.setNewPwd(newPwd);
    }
    
    /**
     * 操作方法
     * @throws AabException 
     */
    @Override
    public Object process(UpdatePwdBo bo) throws AabException {
    	Long userId = bo.getConcurrentUserId();
    	String newPwd = bo.getNewPwd();
    	
    	User record = new User();
    	record.setUserId(userId);
    	record.setPwd(newPwd);
    	userDao.updateByPrimaryKeySelective(record);
    	return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdatePwdBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.修改密码.key());
    	record.setTargetId(param.getConcurrentUserId());
    	record.setTargetName(param.getNewPwd());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
